//
//  LPTwithdrawalViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/6.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTwithdrawalViewController.h"
#import "LPTBankCardTableViewCell.h"
#import "LPTDrawBalanceTableViewCell.h"
#import "LPTWithDrawTableFooterView.h"
#import "LPTAccountWaterViewController.h"

@interface LPTwithdrawalViewController ()<UITableViewDataSource,UITableViewDelegate,LPTWithDrawTableFooterViewDelegate>

@property (nonatomic,copy)NSDictionary *dic;

@end

@implementation LPTwithdrawalViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubViews];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getConfig];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushToTakeoutRecord"]) {
        
        LPTAccountWaterViewController *vc = segue.destinationViewController;
        vc.type = kTakeoutType;
    }
}

-(void)initSubViews
{
    LPTWithDrawTableFooterView *footView = [[LPTWithDrawTableFooterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 400)];
    footView.delegate = self;
    self.tableView.tableFooterView = footView;
    [self.dataSource addObjectsFromArray:@[@[@"银行卡号"],@[@"提现余额"]]];
    [self.tableView reloadData];
}

//获取系统配置
-(void)getConfig
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/common/system_config_list" withBody:@{}];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
       
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.dic = data[@"configs"];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
}

//提现
-(void)getCash
{
    LPTDrawBalanceTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    NSString *balance = [cell getAmmount];
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/cash" withBody:@{@"amount":balance} withMethod:YTKRequestMethodPOST];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
       
        [MMProgressHUD show];
        [MMProgressHUD dismissWithSuccess:@"提现成功"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD show];
        [MMProgressHUD dismissWithError:data];
        
    }];
}

#pragma mark - UITableVIewDataSource & Delegte
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        LPTBankCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTBankCardTableViewCell" forIndexPath:indexPath];
        [cell refreshWithEntity:[[LTPCacheManager shareInstance] getUserInfoModel]];
        
        return cell;
        
    }else{
        
        LPTDrawBalanceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTDrawBalanceTableViewCell" forIndexPath:indexPath];
        [cell refreshWithDic:self.dic];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 67;
    }else{
        return 157;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 && indexPath.section == 0) {
        [self performSegueWithIdentifier:@"pushToEditBankCardVC" sender:nil];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

#pragma mark - LPTWithDrawFooterViewDelegate
-(void)withDrawTableFooterViewDidClickDraw
{
    [self getCash];
}

@end
